<template>
	<view class="u-page">
			<view class="header"></view>
		<view class="content">
			<view class="title">项目权益</view>
			<view class="wenxin">
				<view class="img"><image src="../../../static/icon/wenxin.png" mode=""></image></view>
				<view class="text">权益简要说明</view>
			</view>
			<view class="familyPower" v-if="data.power != null">
				<rich-text :nodes="data.power"></rich-text> 
			</view>
			
			<view class="familyPowerOv" v-else>
				暂无信息
			</view>
			
		</view>
		<view class="footer">
			<button type="warn" @click="rechargeVip()" v-if="isVip != 1">马上充值</button>
			<button type="default"  v-else>已成为家人</button>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				userData:{},
				data:{},
				orderid:"",
				pid:'',
				isVip:"0",
				userhll:false,
			}
		},
		async onLoad(e) {
			this.pid = e.pid
			let user = uni.getStorageSync('userData');
			this.userData = user;
			this.config()
			
		},
		methods: {
			async config(){
				const project = await this.GQL({
					"operationName": "allSlProjects",
					"query": "query allSlProjects($filter: SlProjectFilter) { allSlProjects(filter: $filter) { nodes { title projectFamily power familyRights cashier shopId } } }",
					"variables": {"filter": {"pid": {"equalTo": this.pid}}}
				})
				const hllvip =  await this.GQL({
					"operationName": "allSlUserHllvips",
					"query": "query allSlUserHllvips($filter: SlUserHllvipFilter) { allSlUserHllvips(filter:$filter) { nodes { pid uid id } } }",
					"variables": {"filter": {"pid": {"equalTo": this.pid},"uid": {"equalTo": this.userData.uid}}}
				})
				if(hllvip.data.allSlUserHllvips.nodes !=''){
					this.userhll = true;
				}
				const userData1 = await this.GQL({
					"operationName": "allSlUserProjectvips",
					"query": "query allSlUserProjectvips($filter: SlUserProjectvipFilter) { allSlUserProjectvips(filter:$filter) { nodes { vid uid pid level } } }",
					"variables": {"filter": {"uid": {"equalTo": this.userData.uid},"pid": {"equalTo": this.pid}}}
				})
				
				// console.log('user---',userData1)
				// console.log('user---', this.userData.uid)
				if(userData1.data.allSlUserProjectvips.nodes !=''){
					this.isVip = '1'
				}else{
					this.isVip = '0'
				}
			
			
				if(project.data.allSlProjects.nodes !=''){
					this.data = project.data.allSlProjects.nodes[0]
				}
				console.log('--------data----------',this.data)
			},
			async rechargeVip(){
				let a = {
						"name":"充值会员",
						"total":this.data.projectFamily,
						"time":new Date().getTime()
					}
				
				let data = {
					uid:this.userData.uid,
					data:JSON.stringify(a),
					type:1,
					pid:this.pid,
					totalPrice:this.data.projectFamily,
				};
				// console.log('1-----',data)
				//下单并调起支付
				const order = await this.GQL({
					"operationName": "createSlUserOrder",
					"query": "mutation createSlUserOrder($input: CreateSlUserOrderInput!) { createSlUserOrder(input: $input) { slUserOrder { uid orderId oid } } }",
					"variables": { "input": { "slUserOrder":data } }
				})
				if(order.data.createSlUserOrder.slUserOrder.oid !=''){
					this.orderid = order.data.createSlUserOrder.slUserOrder.oid
		
					//请求后台返回支付数据
					// this.hlluser();
					this.goWxpay();
				}else{
					uni.showToast({ title: '生成订单失败', icon: 'error', duration: 3000 });
				}
			},
			async goWxpay(){
				let that = this
				uni.request({
					url:that.HttpServe+"/WeChat/wxpay",
					method:"POST",
					data:{
						oid:that.orderid
					},
					success(res) {
						console.log('res',res)
					
						if(res.data.code == 200){
							// console.log('10----',res.data.data)
							let parameters = JSON.parse(res.data.data.parameters)
							uni.requestPayment({
							    provider: 'wxpay',
								timeStamp: parameters.timeStamp,
								nonceStr: parameters.nonceStr,
								package: parameters.package,
								signType: 'MD5',
								paySign: parameters.paySign,
								success: function (res1) {
										if(that.data.shopId !='' && that.data.cashier =="1" && that.userhll == false){
											that.hlluser();
										}else{
											uni.reLaunch({
												url:"/pages/project/investment/family?pid="+that.pid
											})
										}
								},
								fail: function (err) {
									uni.showToast({ title: '充值失败！', icon: 'error', duration: 3000 });
									console.log('fail:' + JSON.stringify(err));
								}
							});
						}else{
							uni.showToast({ title: '调起支付失败', icon: 'error', duration: 3000 });
							// uni.navigateBack({
							// 	delta: 1
							// })
						}
						
					}
				})
			},
			async hlluser(){
				let that = this
				uni.request({
					url: that.HttpServe+"/Hll/hllUser",
					data:{"uid":that.userData.uid,"pid":that.pid},
					method:"post",
					success(res2) {
						console.log('res2---',res2);
						if(res2.data.code == 200){
							uni.reLaunch({
								url:"/pages/project/investment/family?pid="+that.pid
							})
						}
						console.log('1111',res2);
						
					},
					fail: function (err) {
						uni.showToast({ title: '充值失败！', icon: 'error', duration: 3000 });
						console.log('fail:' + JSON.stringify(err));
					}
				})
			}
			
		}
		
	}
</script>

<style lang="scss">
	.header{
		width: 100%;
		height: 200px;
		/* color: #FFFFFF; */
		background: linear-gradient(#C7171E, #f0f2f5); /* 标准的语法 */
	}
	.content{
		margin: 15px;
		background-color: #FBFCFE;
		position: relative;
		top: -200px;
		height: 100%;
		border-radius: 5px;
		.title{
			padding-top: 10px;
			margin-left: 10px;
			font-size: 18px;
			font-family: PingFangSC-Semibold, PingFang SC;
			font-weight: 600;
			color: #161E3E;
			line-height: 16px;
		}
		.wenxin{
			margin: 10px;
			height: 50px;
			height: 48px;
			background: #FFF4F5;
			border-radius: 5px;
			border: 1px solid #FFB4B7;
			
		}
		.img{
			position: absolute;
			margin: 10px;
			image{
				width:25px;
				height: 25px;
			}
		}
		
		.text{
			margin: 15px 0px 0px 50px;
			font-size: 12px;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			color: #E73B42;
			line-height: 18px;
		}
		.familyPower{
			font-size: 12px;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			line-height: 16px;
		
		}
		.familyPowerOv{
			font-size: 12px;
			font-family: PingFangSC-Regular, PingFang SC;
			font-weight: 400;
			line-height: 16px;
			text-align: center;
		}
		
	}
		
	.footer{
		position: absolute;
		bottom: 0px;
		background: #fff;
		width: 100%;
		padding: 10px 0px;
		button{
			width: 80%;
		}
	}
</style>
